package com.itzzy.dice.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itzzy.dice.bean.entity.PlayerInfo;
import com.itzzy.dice.bean.entity.RoomInfo;
import com.itzzy.dice.bean.req.LoginReq;
import com.itzzy.dice.config.CaffeineUtils;
import com.itzzy.dice.constant.RequestCode;
import com.itzzy.dice.constant.ResponseCode;
import com.itzzy.dice.utils.CommonSendUtil;
import xyz.noark.core.annotation.Autowired;
import xyz.noark.core.annotation.Controller;
import xyz.noark.core.annotation.controller.ExecThreadGroup;
import xyz.noark.core.annotation.controller.PacketMapping;
import xyz.noark.core.network.Session;
import xyz.noark.core.network.SessionAttrKey;
import xyz.noark.core.network.SessionManager;
import xyz.noark.network.SocketSession;

@Controller(threadGroup = ExecThreadGroup.ModuleThreadGroup)
public class SystemController {

    @Autowired
    private CaffeineUtils caffeineUtils;

    @PacketMapping(opcode = RequestCode.USER_LOGIN, state = Session.State.CONNECTED)
    public void login(SocketSession session, LoginReq loginReq){
        if (session.getState() == Session.State.INGAME){
            CommonSendUtil.sendErrorInfo(session, "当前用户已登录");
            return;
        }
        if (ObjectUtil.isEmpty(loginReq.getUsername().trim())){
            CommonSendUtil.sendErrorInfo(session, "用户名不能为空");
            return;
        }
        if (loginReq.getUsername().trim().contains("-") || loginReq.getUsername().trim().contains("|")){
            CommonSendUtil.sendErrorInfo(session, "用户名不能包含特殊字符");
            return;
        }
        Session player = SessionManager.getSessionByPlayerId(loginReq.getUsername());
        if (player != null){
            CommonSendUtil.sendErrorInfo(session, "当前用户已存在");
            return;
        }

        SessionManager.bindPlayerIdAndSession(loginReq.getUsername(), session);
        session.setPlayerId(loginReq.getUsername());
        session.setState(Session.State.INGAME);
        session.send("", ResponseCode.UI_START + loginReq.getUsername());

        // 校验是否为断线重连用户
        String roomId = caffeineUtils.getRoomIdByPlayerId(loginReq.getUsername());
        if (ObjectUtil.isNotEmpty(roomId)) {
            session.attr(SessionAttrKey.valueOf("roomId")).set(roomId);

            // 发送房间内的信息
            RoomInfo roomInfo = caffeineUtils.getObjCacheByKey(roomId);
            PlayerInfo playerInfo = roomInfo.getAllPlayerMap().get(loginReq.getUsername());
            session.send("", ResponseCode.ROOM_CREATE + roomInfo + "|" + (playerInfo.getIsMaster() ? 1 : 0 ));
            if (roomInfo.getRoomStatus() == 1) {
                session.send("", ResponseCode.DICE_LIST + CollectionUtil.join(playerInfo.getDiceList(), "-"));
            }
            if (roomInfo.getNowPlayerID() != null && roomInfo.getNowPlayerID().equals(loginReq.getUsername())){
                if (roomInfo.getDiceType() == 0) {
                    session.send("", ResponseCode.GAME_START
                            + (roomInfo.getCheckNum() + 1) + "-" + roomInfo.getCheckDice() + "-" + roomInfo.getDiceType());
                }else {
                    session.send("", ResponseCode.GAME_START
                            + (roomInfo.getNormalCheckNum() + 1) + "-" + roomInfo.getNormalCheckDice() + "-" + roomInfo.getDiceType());
                }
            }
        }

    }

}
